import { createRouter, createWebHashHistory} from "vue-router";

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录'
        },
        component: () => import('/src/view/login'),
    },
    {
        path: '/account',
        name: 'account',
        redirect: '/account/workplace',
        meta: {
            title: '后台管理'
        },
        component: () => import('/src/view/account/account'),
        children:[
            {
                path: '/account/workplace',
                name: 'workplace',
                component: ()=> import('/src/view/account/workplace'),
                meta: {
                    title:'工作台',
                },
            },
            {
                path: '/account/userList',
                name: 'userList',
                component: ()=> import('/src/view/account/userList'),
                meta: {
                    title:'用户列表',
                },
            },
            {
                path: '/account/mySetting',
                name: 'mySetting',
                component: () => import('/src/view/account/mySetting'),
                meta: {
                    title: '个人设置'
                }
            },
            {
                path: '/account/activityList',
                name: 'activityList',
                component: () => import('/src/view/account/activity'),
                meta: {
                    title: '活动管理'
                }
            },
            {
                path: '/account/fileList',
                name: 'fileList',
                component: () => import('/src/view/account/fileList'),
                meta: {
                    title: '文件管理'
                }
            },
            {
                path: '/account/addNews',
                name: 'addNews',
                component: () => import('/src/view/account/addNews'),
                meta: {
                    title: '新增通知'
                }
            },
            {
                path: '/account/newsList',
                name: 'newsList',
                component: () => import('/src/view/account/newsList'),
                meta: {
                    title: '通知列表'
                }
            },
            {
                path: '/account/addGoods',
                name: 'addGoods',
                component: () => import('/src/view/account/addGoods'),
                meta: {
                    title: '新增物资'
                }
            },
            {
                path: '/account/goodsList',
                name: 'goodsList',
                component: () => import('/src/view/account/goodsList'),
                meta: {
                    title: '物资列表'
                }
            },
            {
                path: '/account/teamList',
                name: 'teamList',
                component: () => import('/src/view/account/team'),
                meta: {
                    title: '团队列表'
                }
            },
        ]
    }

]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to,from,next) => {
    document.title = '志愿'+ ' ' + '|' + ' ' + to.meta.title
    next()
})


router.beforeEach((to,form,next)=> {
    if (to.path == '/login') {
        sessionStorage.removeItem("vuex");
    }
    let user = JSON.parse(sessionStorage.getItem("vuex"));
    if (!user && to.path != '/login') {
        next({path: '/login'})
    } else {
        next();
    }
})
export default router